<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Recovery extends CrmController
{
    /**
     * 渲染模板recoveryList
     */
    public function recoveryList()
    {
        $menuname = "Recovery List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        $member = Cache::get('member');
        $this->assign("member",$member);
        // 模板输出
        return $this->fetch('recoveryList');
    }


    /**
     * 获取回收资料
     * @return json 回收池资料
     */
    public function recoveryDataList() 
    {
        // $users = Cache::get('users');
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        //需先判断传值是否为空
        $where = "(1 ";
        $where .= " AND recovery = 2";
        $where .= empty($_REQUEST["customer_type"])?" AND customer_type = 'first'":" AND customer_type = '".$_REQUEST["customer_type"]."'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND b.customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND create_user = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["start_recycle_time"])?"":" AND recycle_time > '".$_REQUEST["start_recycle_time"]."'";
        $where .= empty($_REQUEST["end_recycle_time"])?"":" AND recycle_time < '".$_REQUEST["end_recycle_time"]."'";
        $where .= empty($_REQUEST["integral"])?"":" AND integral = '".$_REQUEST["integral"]."'";
        $where .= empty($_REQUEST["member_level"])?"":" AND member_level = '".$_REQUEST["member_level"]."'";
        $where .= empty($_REQUEST["order_num"])?"":" AND order_num = '".$_REQUEST["order_num"]."'";
        $where .= empty($_REQUEST["sign_num"])?"":" AND sign_num = '".$_REQUEST["sign_num"]."'";
        $where .= empty($_REQUEST["age"])?"":" AND age = '".$_REQUEST["age"]."'";
        $where .= empty($_REQUEST["sex"])?"":" AND sex = '".$_REQUEST["sex"]."'";
        $where .= empty($_REQUEST["id_card"])?"":" AND id_card like '%".$_REQUEST["id_card"]."%'";
        
        if ($_REQUEST["last_call_time"]) {
            $last_call_time = explode(',', $_REQUEST["last_call_time"]);
            $where .= $this->lastTime($last_call_time[0], 'last_call_time');
            $where .= $this->lastTime($last_call_time[2], 'last_call_time');
        }

        if ($_REQUEST["last_order_time"]) {
            $last_order_time = explode(',', $_REQUEST["last_order_time"]);
            $where .= $this->lastTime($last_order_time[0], 'last_order_time');
            $where .= $this->lastTime($last_order_time[2], 'last_order_time');
        }
        $where .= " )";
    
        $arrData = Loader::model('Customer')->getCustomerData($where,$page,$limit);
        echo json_encode($arrData);
    }


    protected function lastTime ($time, $key)
    {
        $where = ' ';
        if ($time) {
            $lastTime = date('Y-m-d', time() - substr($time, 1)*24*3600);
            if (substr($time, 0, 1) == '=') {
                $where .= " AND $key like '%".$lastTime."%'";
            } else if (substr($time, 0, 1) == '<') {
                $where .= " AND $key > '".$lastTime."'";
            } else {
                $where .= " AND $key < '".$lastTime."'";
            }
        }
        return $where;
    }


    /**
     * 还原资料
     */
    public function reductionCustomer ()
    {
        $customer_code = $_REQUEST['customer_code'];
        $customer_code = rtrim($customer_code, ',');
        $customer_codes = explode(',', $customer_code);
        $str = '(';
        foreach ($customer_codes as &$v) {
            $str .= "'$v',";
        }
        $str = rtrim($str, ',');
        $str .= ')';
        // $id = implode(',', $customer_codes);
        $where = "customer_code IN $str";
        
        // print_r($where);exit;
        Loader::model('Recovery')->reductionCustomer($where);
    }


    /**
     * 渲染模板shareCustomer
     */
    public function taskCustomer()
    {
        $menuname = "Recovery List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 读缓存 获取回访类型信息
        $task = Cache::get('task');
        $options = "";
        foreach ($task as $key => $val) {
            $options .= "<option value='$key'>";
            $options .= $val['task_type'];
            $options .= "</option>";
        }
        $this->assign('options', $options);
        // 读缓存 获取销售方案信息
        $saleCache = Cache::get('saleProgram');
        $saleProgram = "";
        foreach ($saleCache as $key => $val) {
            $saleProgram .= "<option value='$key'>";
            $saleProgram .= $val['program_name'];
            $saleProgram .= "</option>";
        }
        $this->assign('saleProgram', $saleProgram);

        $customer_codes = rtrim($_GET['customer_codes'], '|');
        // echo $customer_codes;exit;
        $this->assign('customer_code', $customer_codes);
        // 模板输出
        return $this->fetch('taskCustomer');
    }


    /**
     * 渲染模板editCustomer
     */
    public function editCustomer()
    {
        $menuname = "Recovery List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $customer_code = $_REQUEST['customer_code'];
        if (empty($customer_code)) {
            echo "页面出现未知错误！";
            exit;
        }
        $where = 'b.customer_code="'.$customer_code.'"';
        // 基本资料
        $customerData = Loader::model('Customer')->getCustomerData($where);
        // echo $where;exit;
        $customerData = $customerData['data'][0];
        // echo "<pre>";
        // print_r($customerData);exit;
        if ($customerData['sex'] == "男") {
            $customerData['option'] = '<option value="1" selected>男</option> <option value="2">女</option>';
        } elseif ($customerData['sex'] == "女") {
            $customerData['option'] = '<option value="1">男</option> <option value="2" selected>女</option>';
        } else {
            $customerData['option'] = '<option value="1">男</option> <option value="2">女</option>';
        }


        $users = Cache::get('users');
        $customerData['createuser'] = $users[$customerData['create_user']]['cn_name']."（".$customerData['create_user']."）";

        /*echo "<pre>";
        print_r($customerData);exit;*/
        $this->assign('customerData', $customerData);

        $address = Loader::model('Customer')->getAddress($customer_code);
        $this->assign('address', $address);
        // 模板输出
        return $this->fetch('editCustomer');
    }


    /**
     * 新增数据任务 模板
     */
    public function addDataTask ()
    {
        $menuname = "Recovery List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 客户编码
        $this->assign("customer_code",$_GET['customer_code']);
        $this->assign("customer_name",$_GET['customer_name']);
        // 归属人
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 任务编号
        $arrDataTask = Loader::model('Customer')->getDataTask();
        $this->assign("arrDataTask",$arrDataTask);
        return $this->fetch('addDataTask');
    }


    /**
     * 新增回访任务
     */
    public function doTaskCustomer ()
    {
        $data = $_REQUEST;
        $data['createuser'] = $_SESSION['user_info']['username'];
        $data['createtime'] = date('Y-m-d H:i:s');
        $data['endtime'] = $_REQUEST['endtime'].' 23:59:59';
        // print_r($data);exit;

        Loader::model('Customer')->taskCustomer($data);
        // echo json_encode($ids);
    }


    /**
     * 新增数据任务
     */
    public function doAddDataTask ()
    {
        $data = $_REQUEST;
        $data['addtime'] = date('Y-m-d H:i:s');
        // print_r($data);exit;
        Loader::model('Recovery')->addDataTask($data);
    }

}